﻿/*
 * Copyright 2014 Francois Karman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using Quartz.WebManager.Logic;
using Quartz.WebManager.Web.Models;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace Quartz.WebManager.Web.ApiControllers
{
    /// <summary>
    /// Provides management over the scheduled jobs.
    /// </summary>
    [RoutePrefix("api/jobs")]
    public class JobsController : ApiController
    {
        /// <summary>
        /// Gets all the available job instances.
        /// </summary>
        /// <returns>All the keys associated with the existing job instances.</returns>
        [Route("")]
        [HttpGet]
        public IEnumerable<WebJobInstance> GetAll()
        {
            return JobInstanceManager.RetrieveAll().Select(j => WebJobInstance.Create(j, this.Url));
        }

        /// <summary>
        /// Gets all the available job instances for a specific group.
        /// </summary>
        /// <param name="group">The name of the reference group.</param>
        /// <returns>All the job instances associated with the group identified by <paramref name="group"/>.</returns>
        [Route("{group}")]
        [HttpGet]
        public IEnumerable<WebJobInstance> GetAllForGroup(string group)
        {
            return JobInstanceManager.RetrieveAllForGroup(group).Select(j => WebJobInstance.Create(j, this.Url));
        }
    }
}
